package com.guigu.basedservice.utils;

import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

/**
 * @author heyuhang
 * @create 2024/9/27
 */
@Component
public class UniqueNumberGenerator {

    private static Set<String> generatedNumbers = new HashSet<>();

    public static String generateUniqueNumber() {
        String uniqueNumber;
        do {
            // 生成一个UUID并取其部分值
            uniqueNumber = UUID.randomUUID().toString().replace("-", "").substring(0, 11);
        } while (generatedNumbers.contains(uniqueNumber));

        // 添加到集合中以保证唯一性
        generatedNumbers.add(uniqueNumber);
        return uniqueNumber;
    }

    public static void main(String[] args) {
        // 生成10个示例
        for (int i = 0; i < 10; i++) {
            System.out.println(generateUniqueNumber());
        }
    }
}
